import os
import numpy as np
import matplotlib.pyplot as plt
from influxdb import InfluxDBClient

class get_data:
    def __init__(self,c_ip,filepath):
        # 数据库的ip
        self.client_ip = c_ip
        #数据文件目录
        self.filepath = filepath


    # def sql_conn(self):
    #     client = InfluxDBClient(self.client_ip, 8086, 'root', 'huayun2017', 'stackwatch')
    #     # sql = "select * from port_bytes.txt where direction = 'o' and dip = '10.10.10.7'"
    #     # sql = "select value from port_bytes.txt where direction = 'o' and dip = '10.10.10.7'"

    # 遍历指定目录，显示目录下的所有文件文件名
    def eachFile(self,filepath):
        pathDir = os.listdir(filepath)
        for allDir in pathDir:
            child = os.path.join('%s%s' %(filepath,allDir))
            print(child)

    # 查到的数据写入文件
    def writeDatas(self, sql, filename):
        client = InfluxDBClient(self.client_ip, 8086, 'root', 'huayun2017', 'stackwatch')
        document = open(filename, 'w')
        result = client.query(sql)
        document.write('Result:{0}'.format(result))
        document.close()

    # 取出数据，用于备用
    def getBytesData(self,file):
        defaultFile = open(file)
        for line in defaultFile:
            parts = line.split("[")[1].split("]")[0]
            # print('数据流内容：',parts)
        return parts

    # 重新将优化格式的数据写入文件
    def split_data(self,data, file_name):
        list_byte = data.split('},')
        file = open(file_name, 'w+')
        for line in list_byte:
            file.write(line + '\n')


class data_processing:
    def __init__(self,filename):
        self.valus = []
        self.file_name = filename

    def processing(self):
        data = open(self.file_name)
        for each_data in data:
            (time, value) = each_data.split(',')
            time = time.replace("{", "")
            value = value.replace("}", "")
            # 取比特值
            value = value.split(':')[1]
            # 去除\n
            value = value.replace("\n", "")
            # 去除空格
            # print(time)    value = value.replace(" ","")
            # print()
            # print(value)
            v = round(int(value) / 1000 / 1000)
            self.valus.append(v)

        data.close()
        print(self.valus)
        return self.valus

    def show_data(self):
        x_value = self.valus
        lenth = len(self.valus)
        # print(lenth)
        index = np.arange(lenth)

        # index = 10
        plt.bar(left=index, height=x_value, color='gray', width=0.5)

        plt.show()





if __name__ == '__main__':
    # 流量大小
    unit = '10M'
    #文件目录
    filepath = os.getcwd()+'\\data\\'

    # 数据库所在的ip
    cip = '172.16.35.200'

    # 东西流量文件
    port_file = 'port_bytes_'+unit+'.txt'
    new_port_file = 'new_port_bytes_'+unit+'.txt'

    # 南北流量文件
    fip_file = 'fip_bytes_'+unit+'.txt'
    new_fip_file = 'new_fip_bytes_'+unit+'.txt'

    # 赋值
    bytes_file = port_file
    new_bytes_file = new_port_file

    # 获取数据初始化
    g =get_data(c_ip=cip)

    sql = "select * from port_bytes.txt where direction = 'o' and dip = '10.10.10.7'"
    # sql = "select value from port_bytes.txt where direction = 'o' and dip = '10.10.10.7'"

    # 将查询的数据写入文件
    g.writeDatas(sql,bytes_file)
    # 取出数据，用于备用
    bytes_data = g.getBytesData(bytes_file)
    # 重新将优化格式的数据写入文件
    g.split_data(bytes_data,new_bytes_file)


    # 取出优化后的数据，再处理后展现处理后的数据
    d = data_processing(filename=new_bytes_file)

    data = d.processing()
    image_data = d.show_data()

